package cn.anzhiyublog.staranimegallery.config;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.region.Region;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * COS配置，从配置文件中获取
 *
 * @author 安知一只鱼
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "cos")
public class COSConfig {
    /**
     * 域名
     */
    private String host;

    /**
     * 密钥id
     */
    private String secretId;

    /**
     * 密钥key
     */
    private String secretKey;

    /**
     * 地区
     */
    private String region;

    /**
     * 桶名
     */
    private String bucket;

    @Bean
    public COSClient getCOSClient(){
        // 使用密钥初始化身份信息
        BasicCOSCredentials credentials = new BasicCOSCredentials(secretId, secretKey);
        // 设置COS桶地区
        ClientConfig config = new ClientConfig(new Region(region));
        // 初始化cos客户端
        return new COSClient(credentials, config);
    }
}
